import Mock from 'mockjs'

// 模拟用户数据
const userList = [
    {
        id: '0aa01bb4d3f7423f9e11336cf5ca6ef5',
        deptId: '88921a7b70a04ea2b7d1db1651ffe666',
        name: 'admin',
        email: '159xx37442@qq.com',
        phoneNumber: '17625xx1384',
        sex: '1',
        password: '',
        avatar: null,
        status: 1,
        loginIp: null,
        loginTime: null,
        createBy: 'admin',
        createTime: '2024-08-24 21:43:28',
        updateBy: null,
        updateTime: null,
        remark: null
    },
    {
        id: '9057fae81b7345e2ac725346278fb9bb',
        deptId: '88921a7b70a04ea2b7d1db1651ffe666',
        name: '测试用户',
        email: '12345678@qq.com',
        phoneNumber: '12345678901',
        sex: '2',
        password: '',
        avatar: null,
        status: 1,
        loginIp: null,
        loginTime: null,
        createBy: 'admin',
        createTime: '2024-03-09 13:27:33',
        updateBy: null,
        updateTime: null,
        remark: null
    }
]

export default [
    // 查询用户列表（分页）
    {
        url: '/dev-api/system/block/user/list',
        type: 'post',
        response: (config) => {
            const { pageNum = 1, pageSize = 10, name, status } = config.body

            // 过滤条件
            let filteredList = userList.filter((item) => {
                // 名称筛选（模糊匹配）
                if (name && !item.name.includes(name)) {
                    return false
                }
                // 状态筛选（精确匹配）
                if (status !== undefined && status !== '' && item.status !== Number(status)) {
                    return false
                }
                //处理性别说明和状态说明
                item.sexDesc = item.sex === 1 ? '男' : '女'
                item.statusDesc = item.status === 1 ? '启用' : '禁用'
                return true
            })

            // 计算分页数据
            const total = filteredList.length
            const totalPage = Math.ceil(total / pageSize)
            const currentPage = Number(pageNum)
            const pageSizeNum = Number(pageSize)

            // 分页
            const start = (currentPage - 1) * pageSizeNum
            const end = currentPage * pageSizeNum
            const pageList = filteredList.slice(start, end)

            return {
                code: 1,
                msg: 'success',
                total: total, // 总条数
                totalPage: totalPage, // 总页数
                currentPage: currentPage, // 当前页码
                pageSize: pageSizeNum, // 页面大小
                data: pageList // 当前页数据
            }
        }
    },

    // 新增用户信息
    {
        url: '/dev-api/system/block/user/addUser',
        type: 'post',
        response: (config) => {
            const data = config.body
            const newUser = {
                id: Mock.mock('@guid'),
                ...data,
                createTime: new Date().toISOString(),
                updateTime: null,
                createBy: 'mock-user',
                updateBy: null,
                status: 1
            }
            userList.unshift(newUser)

            return {
                code: 1,
                msg: '新增成功'
            }
        }
    },

    // 编辑用户信息
    {
        url: '/dev-api/system/block/user/editUser',
        type: 'put',
        response: (config) => {
            const data = config.body
            const index = userList.findIndex((item) => item.id === data.id)

            if (index === -1) {
                return {
                    code: 500,
                    msg: '用户不存在'
                }
            }

            userList[index] = {
                ...userList[index],
                ...data,
                updateTime: new Date().toISOString(),
                updateBy: 'mock-user'
            }

            return {
                code: 1,
                msg: '修改成功'
            }
        }
    },

    // 编辑用户状态
    {
        url: '/dev-api/system/block/user/editUserStatus',
        type: 'put',
        response: (config) => {
            const { id, status } = config.body
            const index = userList.findIndex((item) => item.id === id)

            if (index === -1) {
                return {
                    code: 500,
                    msg: '用户不存在'
                }
            }

            userList[index].status = status
            userList[index].updateTime = new Date().toISOString()
            userList[index].updateBy = 'mock-user'

            return {
                code: 1,
                msg: '状态修改成功'
            }
        }
    },

    // 删除用户信息
    {
        url: '/dev-api/system/block/user/deleteUser/:userId',
        type: 'delete',
        response: (config) => {
            const { userId } = config.query
            const index = userList.findIndex((item) => item.id === userId)

            if (index === -1) {
                return {
                    code: 500,
                    msg: '用户不存在'
                }
            }

            userList.splice(index, 1)

            return {
                code: 1,
                msg: '删除成功'
            }
        }
    }
]
